package com.mcxx.modules.miniSalvation.order.service.write;

import com.google.common.collect.Lists;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.order.dao.read.NoticeBoardDetailReadDao;
import com.mcxx.modules.miniSalvation.order.dao.read.NoticeBoardReadDao;
import com.mcxx.modules.miniSalvation.order.dao.write.NoticeBoardDetailWriteDao;
import com.mcxx.modules.miniSalvation.order.dto.OrderListForChoiceDTO;
import com.mcxx.modules.miniSalvation.order.entity.NoticeBoardDetailEntity;
import com.mcxx.modules.miniSalvation.order.param.NoticeBoardSearchOrderParam;
import com.mcxx.modules.miniSalvation.order.param.OrderListSearchParam;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.modules.temporary.order.dto.TmpOrderDTO;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.modules.temporary.order.service.write.TmpOrderWriteService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("noticeBoardDetailWriteService")
@TaTransactional
public class NoticeBoardDetailWriteService extends BaseService {

    @Autowired
    private NoticeBoardDetailWriteDao noticeBoardDetailWriteDao;
    @Autowired
    private NoticeBoardDetailReadDao noticeBoardDetailReadDao;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;
    
    public int insertObject(List<NoticeBoardDetailEntity> noticeBoardDetailList){
        return noticeBoardDetailWriteDao.inserts(noticeBoardDetailList);
    }

    
    public ExcelInfo exportExcelNoticeMember(OrderListSearchParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);

        List<OrderListForChoiceDTO> list = noticeBoardDetailReadDao.queryObject(param);
        if(Constant.BusinessCode.LINSHI.equals(param.getBusinessType())){
            excelInfo = exportExcelService.exportOrgOpLogs(OrderListForChoiceDTO.class,list,"公示对象信息.xls",
                    "公示对象列表","\"公示对象列表\"",false,"isObjection","checkResult","saveDate","remark");
        }else{
            excelInfo = exportExcelService.exportOrgOpLogs(OrderListForChoiceDTO.class,list,"公示对象信息.xls",
                    "公示对象列表","公示对象列表",false,null);
        }
        if(null != excelInfo){
            InputStream sbs = new ByteArrayInputStream(excelInfo.getFile());
            String objectkey = fileService.upload(sbs,excelInfo.getFileName());
            if(StringUtils.isNotBlank(objectkey)){
                TaskExportEntity taskExport = new TaskExportEntity();
                taskExport.setId(Constant.getUUID());
                taskExport.setUserid(accountVo.getUserId());
                taskExport.setTaskName(excelInfo.getFileName());
                taskExport.setTaskTitle("公示列表");
                taskExport.setParam("xls");
                taskExport.setCreateTime(new Date());
                taskExport.setFileUrl(objectkey);
                taskExport.setState("02");
                int i = taskExportWriteService.insertIgnoreNull(taskExport);
                if(i > 0){
                    excelInfo = null;
                }
            }
        }
        return excelInfo;
    }


    
    public int deleteByOrderId(String orderId){
        return noticeBoardDetailWriteDao.deleteByOrderId(orderId);
    }

    
    public int updateBatchId(List<NoticeBoardDetailEntity> batchNoList){return noticeBoardDetailWriteDao.updateBatchId(batchNoList); }
}
